﻿#include <QSqlError>
#include "GoodsStandard.h"
#include "DatabaseMgmt.h"


CGoodsStandard::CGoodsStandard()
{
	init();
}

CGoodsStandard::~CGoodsStandard()
{

}
void CGoodsStandard::init()
{

}

int CGoodsStandard::insertRow(GoodsStandard standard)
{
	int ret = 0;

	QString strSql = QString("INSERT INTO TB_GOODS_STANDARD (KIND_ID, NAME, MIN_VALUE, MAX_VALUE, MAINLY) "
		"VALUES (%1, '%2', %3, %4, %5)")
		.arg(standard.kind_id)
		.arg(standard.name)
		.arg(standard.min_value)
		.arg(standard.max_value)
		.arg(standard.mainly);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CGoodsStandard::queryAll(QVector<GoodsStandard>& standards)
{
	int ret = 0;
	QString strSql = QString("SELECT UID, KIND_ID, NAME, MIN_VALUE, MAX_VALUE, MAINLY FROM TB_GOODS_STANDARD");

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			GoodsStandard standard;

			standard.uid = query.value("UID").toUInt();
			standard.kind_id = query.value("KIND_ID").toUInt();
			standard.name = query.value("NAME").toString();
			standard.min_value = query.value("MIN_VALUE").toDouble();
			standard.max_value = query.value("MAX_VALUE").toDouble();
			standard.mainly = query.value("MAINLY").toUInt();

			standards.append(standard);
		}
	}

	return ret;
}

int CGoodsStandard::queryByID(quint32 uid, GoodsStandard & standard)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, KIND_ID, NAME, MIN_VALUE, MAX_VALUE, MAINLY FROM TB_GOODS_STANDARD WHERE UID=%1")
		.arg(uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.next())
	{
		standard.uid = query.value("UID").toUInt();
		standard.kind_id = query.value("KIND_ID").toUInt();
		standard.name = query.value("NAME").toString();
		standard.min_value = query.value("MIN_VALUE").toDouble();
		standard.max_value = query.value("MAX_VALUE").toDouble();
		standard.mainly = query.value("MAINLY").toUInt();
	}
	else
	{
		if (query.lastError().type() != QSqlError::NoError)
		{
			ret = CONST_ERR_EXEC_SQL_FAILED;
		}
	}

	return ret;
}

int CGoodsStandard::removeRow(quint32 uid)
{
	int ret = 0;

	QString strSql = QString("DELETE FROM TB_GOODS_STANDARD WHERE UID=%1")
		.arg(uid);
	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);
	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}

	return ret;
}

int CGoodsStandard::queryByKindId(quint32 kindId, QVector<GoodsStandard>& standards)
{
	int ret = 0;

	QString strSql = QString("SELECT UID, KIND_ID, NAME, MIN_VALUE, MAX_VALUE, MAINLY"
		" FROM TB_GOODS_STANDARD WHERE KIND_ID = %1")
		.arg(kindId);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	else
	{
		while (query.next())
		{
			GoodsStandard standard;

			standard.uid = query.value("UID").toUInt();
			standard.kind_id = query.value("KIND_ID").toUInt();
			standard.name = query.value("NAME").toString();
			standard.min_value = query.value("MIN_VALUE").toDouble();
			standard.max_value = query.value("MAX_VALUE").toDouble();
			standard.mainly = query.value("MAINLY").toUInt();

			standards.append(standard);
		}
	}

	return ret;
}

